package com.stylefeng.guns.rest.modular.wz.api.v3;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.EnWz;
import com.stylefeng.guns.rest.enums.EnWzAdv;
import com.stylefeng.guns.rest.enums.EnWzSysFile;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.WzAdvMapServiceImpl;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.WzCommonServiceImplV3;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;


/**
 * 公共api
 **/
@RestController
@RequestMapping("/wz/api/v3/common/")
public class WzCommonApiV3 extends ApiBaseController {

    @Autowired
    private PfSysFileMapper sysFileMapper;
    @Autowired
    private WzAdvMapServiceImpl wzAdvMapService;
    @Autowired
    private WzCommonServiceImplV3 commonServiceImplV3;
    @Autowired
    private WzAdvMapCategoryMapper advMapCategoryMapper;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzUserInfoMapper userInfoMapper;

    @PostMapping("test")
    public int testTwo() throws Exception{
        List<WzUser> users = userMapper.selectList(new EntityWrapper<WzUser>().notLike("avatar_url","http"));
        for(WzUser user:users){
            user.setAvatarUrl("http://oss.50waizhuan.com/" + user.getAvatarUrl());
            userMapper.updateById(user);
        }
        return users.size();
    }

    @PostMapping("hasAdvMap")
    public int test() throws Exception{
        List<WzAdvMap> advMaps = advMapMapper.selectList(new EntityWrapper<WzAdvMap>());
        for(WzAdvMap advMap:advMaps){
            WzUser user = cacheService.user(advMap.getUserCode());
            user.setHasAdvMap(1);
            userMapper.updateById(user);
        }
        return 1;
    }

    @PostMapping("advMapEndTime")
    public int advMapEndTime() throws Exception{
        List<WzAdvMap> advMaps = advMapMapper.selectList(new EntityWrapper<WzAdvMap>());
        for(WzAdvMap advMap:advMaps){
            if(advMap.getEndTime() != null){
                String a = DateUtil.toStringDataTime(advMap.getEndTime());
                String b = a.substring(11,19);
                if(!b.equals("23:59:59")){
                    String c = DateUtil.toString(advMap.getEndTime()) + " 23:59:59";
                    Date d = DateUtil.toDateTime(c);
                    advMap.setEndTime(d);
                    advMapMapper.updateById(advMap);
                }
            }else{
                System.out.println("a");
            }
        }
        return 1;
    }

    /**
     * 主页图片
     */
    @PostMapping("indexPics")
    public Res indexPics(@RequestBody ParamMap paramMap){
        //版本判断
        String androidVersion = paramMap.getStrValid("androidVersion");
        String iosVersion = paramMap.getStrValid("iosVersion");
        int ios = 0;
        int android = 0;
        if(org.apache.commons.lang.StringUtils.isNotBlank(iosVersion)){
            ios = Integer.parseInt(iosVersion.substring(0,1))*100 + Integer.parseInt(iosVersion.substring(2,3))*10 + Integer.parseInt(iosVersion.substring(4,5));
        }
        if(org.apache.commons.lang.StringUtils.isNotBlank(androidVersion)){
            android = Integer.parseInt(androidVersion);
        }
        if(android > 30 || ios > 150){//新务处理
            //业务处理
            Res res = commonServiceImplV3.newIndexPics(paramMap);
            return res;
        }else{//旧业务处理
            //业务处理
            Res res = commonServiceImplV3.indexPics(paramMap);
            return res;
        }
    }

    /**
     * 主页周边推荐
     */
    @PostMapping("indexAdvMapRecommend")
    public Res indexAdvMapRecommend(@RequestBody ParamMap paramMap) {
        //版本判断
        String androidVersion = paramMap.getStrValid("androidVersion");
        String iosVersion = paramMap.getStrValid("iosVersion");
        int ios = 0;
        int android = 0;
        if(org.apache.commons.lang.StringUtils.isNotBlank(iosVersion)){
            ios = Integer.parseInt(iosVersion.substring(0,1))*100 + Integer.parseInt(iosVersion.substring(2,3))*10 + Integer.parseInt(iosVersion.substring(4,5));
        }
        if(org.apache.commons.lang.StringUtils.isNotBlank(androidVersion)){
            android = Integer.parseInt(androidVersion);
        }
        if(android > 30 || ios > 150){//新务处理
            Res res = commonServiceImplV3.newIndexAdvMapRecommend(paramMap);
            return res;
        }else{//旧业务处理
            Res res = commonServiceImplV3.indexAdvMapRecommend(paramMap);
            return res;
        }
    }

}
